package com.lianniu.web.demand.service.impl;

import com.lianniu.web.base.BaseResult;
import com.lianniu.web.buyerProduct.model.BuyerInfoVo;
import com.lianniu.web.buyerProduct.service.IBuyerInfoService;
import com.lianniu.web.demand.dao.DemandDao;
import com.lianniu.web.demand.service.DemandDetailService;
import com.lianniu.web.demand.service.DemandService;
import com.lianniu.web.demand.vo.DemandDetailVo;
import com.lianniu.web.demand.vo.DemandVo;
import com.lianniu.web.product.model.Page;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2017-05-14.
 */
@Service
public class DemandServiceImpl implements DemandService {

    @Autowired
    private DemandDao demandDao;
    @Autowired
    IBuyerInfoService buyerInfoService;
    @Autowired
    DemandDetailService demandDetailService;

    @Override
    public BaseResult isAuthed(String userid) {
        BaseResult buyerInfoByUserId = buyerInfoService.getBuyerInfoByUserId(userid);
        BuyerInfoVo buyerInfoVo = (BuyerInfoVo) buyerInfoByUserId.getData();
        if ("1".equals(buyerInfoVo.getBuyerStatus()))
        {
            return BaseResult.success("no");
        }

        if ("2".equals(buyerInfoVo.getBuyerStatus()))
        {
            return BaseResult.success("yes");
        }
        return BaseResult.success("no");
    }

    @Override
    public BaseResult insert(DemandVo record) {
        int in = this.demandDao.insert(record);
        return BaseResult.success(in);
    }

    @Override
    public BaseResult insertSelective(DemandVo record) {
        int in = this.demandDao.insertSelective(record);
        return BaseResult.success(in);
    }

    @Override
    public BaseResult selectByPrimaryKey(Long id) {
        DemandVo demandVo = this.demandDao.selectByPrimaryKey(id);
        List<DemandDetailVo> demandDetailVos = (List<DemandDetailVo>)this.demandDetailService.selectByDemandId(demandVo.getId()).getData();
        demandVo.setDetails(demandDetailVos);

        return BaseResult.success(demandVo);
    }

    @Override
    public BaseResult selectByUserId(@Param("userId") Long userId,int pageNum) {
        int count = this.demandDao.selectByUserIdCount(userId);
        Page page = new Page(count, pageNum);
        List<DemandVo> list = this.demandDao.selectByUserId(userId,page);
        for (DemandVo demandVo : list)
        {
            demandVo.setDetails((List<DemandDetailVo>) this.demandDetailService.selectByDemandId(demandVo.getId()).getData());
        }
        return BaseResult.success(list);
    }

    @Override
    public BaseResult findSource(String userId,int pageNum) {
        int count = this.demandDao.findSourceCount(userId);
        Page page = new Page(count, pageNum);
        List<DemandVo> list = this.demandDao.findSource(userId,page);
        for (DemandVo demandVo : list)
        {
            demandVo.setDetails((List<DemandDetailVo>) this.demandDetailService.selectByDemandId(demandVo.getId()).getData());
        }
        return BaseResult.success(list);
    }

    @Override
    public BaseResult updateByPrimaryKeySelective(DemandVo record) {
        int up = this.demandDao.updateByPrimaryKeySelective(record);
        return BaseResult.success(up);
    }

    @Override
    public BaseResult updateByPrimaryKey(DemandVo record) {
        int up = this.demandDao.updateByPrimaryKey(record);
        return BaseResult.success(up);
    }

    @Override
    public BaseResult increaseSupplyCount(@Param("id") Long id) {
        this.demandDao.increaseSupplyCount(id);
        return BaseResult.success();
    }

    @Override
    public BaseResult increaseClickCount(@Param("id") Long id) {
        this.demandDao.increaseClickCount(id);
        return BaseResult.success();
    }
}
